package SSM.Chat.Listener;

import SSM.Chat.Dao.BookDao;
import SSM.Chat.Domain.AuthCode;
import SSM.Chat.Domain.Settings;
import SSM.Chat.Service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


//@WebListener // 将该类配置为监听器  代替web.xml
// 清理超过十分钟的验证码
@Component
public class AutoCodeClear  {

    @Autowired
    private BookDao bookDao;

    @PostConstruct
    public void contextInitialized() {
        try {
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    List<AuthCode> authCodes = bookDao.AutoCodeSelectAll();
                    if(authCodes.size()>0){
                        authCodes.forEach(el->{
                            if(el.getAuthCodeTime().getTime()+1000*60*10<new Date().getTime()){
                                bookDao.DelAutoCode(el);
                            }
                        });
                    }


                }
            }, 0, 1000);

        } catch (Exception e) {
//                   LOGGER.error("GreyClientInitListener error", e);
            System.out.println(e);
        }

    }
}
